//
//  TMGSaleGoldViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/12.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGBootomGoldInputViewController.h"
#import "NSCalendar+STCalendarTool.h"
@interface TMGBootomGoldInputViewController ()
@property(nonatomic, strong) UITextField                     *textFiled;
@property(nonatomic, strong) NSArray                        *dataSouce;
@property(nonatomic, strong) STLabel     *moneyLable;
@end

@implementation TMGBootomGoldInputViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [TMUtils fetchPersonnalInfo:^(TMGUserModel *user) {
        
    }];
    [self configDataSouce];
    self.title = @"转入";
    [self configSubView];
    [self configTableFooter];
}
- (void)configDataSouce{
    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0.66*UIScreenWidth, 44)];
    self.textFiled.placeholder = @"请输入重量";
    self.textFiled.keyboardType = UIKeyboardTypeDecimalPad;
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"购买克重" accessoryView:self.textFiled]];
    [self.tableView reloadData];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor clearColor];
    
    STLabel * saleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)
                                                    text:self.detailModel.name
                                               textColor:TM_firstTextColor
                                                    font:15
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentCenter];
    saleLable.backgroundColor = [UIColor clearColor];
    [header addSubview:saleLable];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 44, UIScreenWidth, 80)
                                                     text:[NSString stringWithFormat:@"压箱金(克)\n%@",
                                                           @"2.00"]
                                                textColor:TM_orangeColor
                                                     font:26
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerX = UIScreenWidth / 2;
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"压箱金(克)"
                                                                           attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:FirstTextColor}];
    self.moneyLable = titleLable;
    titleLable.backgroundColor = [UIColor whiteColor];
    [header addSubview:titleLable];

    

    
    
    [TMUtils fetchPersonnalInfo:^(TMGUserModel *user) {
        titleLable.text = [NSString stringWithFormat:@"压箱金(克)\n%0.2f",
                           user.gold.floatValue];
        titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"压箱金(克)"
                                                                               attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:FirstTextColor}];
    }];
    header.height = titleLable.bottom;
    self.tableView.tableHeaderView = header;
    
}
- (void)configTableFooter{
    
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;

    
    STButton * nextButton = [[STButton alloc] initWithFrame:CGRectMake(30, 20, UIScreenWidth - 60, 49)
                                                      title:@"确定转入"
                                                 titleColor:FlatWhite
                                                  titleFont:15
                                               cornerRadius:6
                                            backgroundColor:TM_orangeColor
                                            backgroundImage:nil
                                                      image:nil];
    nextButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [nextButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedNextButton];
    }];
    [footer addSubview:nextButton];
    self.tableView.tableFooterView = footer;
    
    
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.accessoryType = UITableViewCellAccessoryNone;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.detailTextLabel.text = model.detailString;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelectedAlertButton{
    
}
//确定下单
- (void)onSelectedNextButton{

    if (self.textFiled.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"您还没输入重量哦"];
        return;
    }
    
    [self presentInputPwdViewController];

}
#pragma mark --NetWork Method
- (void)presentInputPwdViewController{
    LBPassWordPresentViewController * vc = [LBPassWordPresentViewController new];
    [vc setPwdDidEdited:^(LBPassWordPresentViewController *passVC ,NSString *inputPwd) {
        [self sendBuyRequestWithPwd:inputPwd handle:^(NSString *errorDesc) {
            if (!errorDesc.length) {
                [SVProgressHUD showSuccessWithStatus:@"购买成功"];
                [passVC dismissWithHandle:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }else{
                if ([errorDesc isEqualToString:@"交易密码不正确!"]) {
                    passVC.alertLable.text = @"密码错误";
                    passVC.alertLable.hidden = NO;
                    [passVC rightLablebackAnimation];
                }else{
                    [SVProgressHUD showErrorWithStatus:errorDesc];
                }
                
            }
        }];
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)sendBuyRequestWithPwd:(NSString*)pwd handle:(void(^)(NSString * errorDesc))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/order/addOrderFloat"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (pwd.length) {
        [dic setObject:pwd forKey:@"pay_password"];
    }
    if (self.textFiled.text.length) {
        [dic setObject:self.textFiled.text forKey:@"goods_number"];
    }
    NSDate * date = [NSDate st_localCurrentDate];
     [dic setObject:date.st_yyyyMMddByLineWithDate forKey:@"start"];
    for (NSInteger i = 0; i < self.detailModel.day_number.integerValue; i++) {
        date = [NSCalendar st_nextDayDate:date];
    }

    [dic setObject:self.detailModel.goodID forKey:@"gid"];
    [dic setObject:self.detailModel.year_rate_from forKey:@"year_rate"];
    [dic setObject:date.st_yyyyMMddByLineWithDate forKey:@"end"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 if (handle) {
                                                     handle(@"");
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(error.desc);
                                                 }
                                             }];
}

@end



